class_name ShopRelic
extends  VBoxContainer


@export var relic: Relic : set = _set_relic

@onready var card_container: CenterContainer = %CardContainer
@onready var price_label: Label = %PriceLabel
@onready var buy_button: Button = %BuyButton
@onready var gold_cost := RNG.instance.randi_range(100, 300)
@onready var visuals: Node2D = %Visuals


func update(run_stats: RunStats) -> void:
	if not price_label or not buy_button:
		return
	price_label.text = str(gold_cost)
	if run_stats.gold >= gold_cost:
		price_label.remove_theme_color_override("font_color")
		buy_button.disabled = false
	else:
		price_label.add_theme_color_override("font_color", Color.RED)
		buy_button.disabled = true
		
func _set_relic(value: Relic) -> void:
	relic = value
	if not is_node_ready():
		await ready
	for child in card_container.get_children():
		child.queue_free()
	var new_relic_ui : RelicUI = AutoLoader.RELIC_UI_SCENE.instantiate()
	card_container.add_child(new_relic_ui)
	new_relic_ui.relic = relic


func _on_buy_button_pressed() -> void:
	Events.do_shop_relic_bought(relic, gold_cost)
	visuals.visible = false
